package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.ApprovalProjectAdapter;
import com.wnhz.work.yz.bean.JobProjectBean;
import com.wnhz.work.yz.fragment.project.JobAllFragment;
import com.wnhz.work.yz.fragment.project.JobApproveFragment;
import com.wnhz.work.yz.fragment.project.JobRejectFragment;

import java.util.ArrayList;
import java.util.List;

public class JobauditActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private TextView text1, text2, text3;
    private View line1, line2, line3;
    private int textColorNor, textColorCli;

    private JobAllFragment fragment1;
    private JobApproveFragment fragment2;
    private JobRejectFragment fragment3;

    private List<Fragment> fragmentList;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private String type;
    private String Page;
    private ApprovalProjectAdapter adapter;
    private List<JobProjectBean> jobBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jobaudit);
        initData();
        initView();
    }

    private void initData() {
        textColorNor = getResources().getColor(R.color.text_two);
        textColorCli = getResources().getColor(R.color.main_color);
        type = getIntent().getStringExtra("type");
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("特批");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        text1 = (TextView) findViewById(R.id.tv_alljob);
        text2 = (TextView) findViewById(R.id.tv_approve);
        text3 = (TextView) findViewById(R.id.tv_dispose);

        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);
        line3 = findViewById(R.id.line3);

        text1.setOnClickListener(this);
        text2.setOnClickListener(this);
        text3.setOnClickListener(this);

        fragmentList = new ArrayList<>();
        fragment1 = new JobAllFragment();
        fragment1.setContext(this);
        fragment1.setType("0");
        fragment2 = new JobApproveFragment();
        fragment2.setContext(this);
        fragment2.setType("1");
        fragment3 = new JobRejectFragment();
        fragment3.setContext(this);
        fragment3.setType("2");

        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);

        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fl_jobproject, fragment1);
        transaction.add(R.id.fl_jobproject, fragment2);
        transaction.add(R.id.fl_jobproject, fragment3);

        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.commit();
        if ("0".equals(Page)) {
            transaction.hide(fragment2);
            transaction.hide(fragment3);
            transaction.commit();
            onClick(text1);
        } else if ("1".equals(Page)) {
            transaction.hide(fragment1);
            transaction.hide(fragment3);
            transaction.commit();
            onClick(text2);
        } else if ("2".equals(Page)) {
            transaction.hide(fragment1);
            transaction.hide(fragment2);
            transaction.commit();
            onClick(text3);

        }
        // upload("0");
    }

    private void hideFragment(int index) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            if (i == index) {
                transaction.show(fragmentList.get(i));
            } else {
                transaction.hide(fragmentList.get(i));
            }
        }
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Intent intent;
//        Toast.makeText(JobauditActivity.this, "" + v.getId(), Toast.LENGTH_SHORT).show();
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //全部
            case R.id.tv_alljob:
                hideFragment(0);
                transaction.show(fragmentList.get(0)).commit();
                text1.setTextColor(textColorCli);
                text2.setTextColor(textColorNor);
                text3.setTextColor(textColorNor);

                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                //   upload("0");
                //    Toast.makeText(JobauditActivity.this, "0", Toast.LENGTH_SHORT).show();
                break;

            //待审批
            case R.id.tv_approve:
                hideFragment(1);
                transaction.show(fragmentList.get(1)).commit();
                text1.setTextColor(textColorNor);
                text2.setTextColor(textColorCli);
                text3.setTextColor(textColorNor);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.VISIBLE);
                line3.setVisibility(View.INVISIBLE);
                //   upload("1");
                //    Toast.makeText(JobauditActivity.this, "1", Toast.LENGTH_SHORT).show();
                break;

            //已处理
            case R.id.tv_dispose:
                hideFragment(2);
                transaction.show(fragmentList.get(2)).commit();
                text1.setTextColor(textColorNor);
                text2.setTextColor(textColorNor);
                text3.setTextColor(textColorCli);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.VISIBLE);
                //   Toast.makeText(JobauditActivity.this, "2", Toast.LENGTH_SHORT).show();
                //  upload("2");
            default:
                break;
        }
    }
//
//    private static final String TAG = "JobauditActivity";
//
//   public void upload(final String type) {
//        showDialog();
//        Map<String, Object> map = new HashMap<>();
//        map.put("token", MyApplication.getInstance().getUserBean().getToken());
//        map.put("type", type);
//       //Toast.makeText(JobauditActivity.this, MyApplication.getInstance().getUserBean().getToken()+"--"+type, Toast.LENGTH_SHORT).show();
//        XUtil.Post(Url.ORDER_GETOSHENPINGTEPI, map, new MyCallBack<String>() {
//            @Override
//            public void onSuccess(String result) {
//                closeDialog();
//                super.onSuccess(result);
//                Log.i(TAG, result);
////                Log.i(TAG, "onSuccess: AllOrderActivity= " +result.substring(0, result.length() / 2));
////                Log.i(TAG, "onSuccess: AllOrderActivity= " + result.substring(result.length() / 2, (result.length() / 2 * 1)));
////                Log.i(TAG, "onSuccess: AllOrderActivity= " + result.substring((result.length() / 2 * 1), result.length()));
//                try {
//                    JSONObject jsonObject = new JSONObject(result);
//                    String res = jsonObject.optString("result");
//                    JSONArray jsonArray = jsonObject.optJSONArray("info");
//                    Log.e("-------", res);
//
//
//                    if (jsonArray != null) {
//                        jobBeanList.clear();
//                        for (int i = 0; i < jsonArray.length(); i++) {
//                            JobProjectBean bean = new JobProjectBean();
//                            Gson gson = new Gson();
//                            bean = gson.fromJson(jsonArray.optString(i), JobProjectBean.class);
//                            jobBeanList.add(bean);
//                        }
//                        Log.i(TAG, "initView:==1----------- " + jobBeanList.size());
//                    }
//                    if ("0".equals(type)) {
//                        fragment1.setData(jobBeanList);
//                    } else if ("1".equals(type)) {
//                        fragment2.setData(jobBeanList);
//                    } else if ("2".equals(type)) {
//                        fragment3.setData(jobBeanList);
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//
//
//            }
//
//            @Override
//            public void onFinished() {
//                closeDialog();
//                super.onFinished();
//            }
//
//            @Override
//            public void onError(Throwable ex, boolean isOnCallback) {
//                closeDialog();
//                super.onError(ex, isOnCallback);
//                Log.e(TAG, "onError: " + ex);
//            }
//        });
//    }
}
